﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Food
{
    public class FoodLike : IFoodLike
    {
        public void LikeFood(int FoodId, int userId, bool like, ref Model.NutriLeafDBEntities context)
        {
            Model.FoodLike likefood = new Model.FoodLike();
            likefood.Liked = like;
            likefood.UserID = userId;
            likefood.FoodID = FoodId;
            context.FoodLikes.AddObject(likefood);
            context.SaveChanges();
        }

        public void LikeFood(Model.Food f, Model.User user, bool like, ref Model.NutriLeafDBEntities context)
        {
            Model.FoodLike likefood = new Model.FoodLike();
            likefood.User = user;
            likefood.Food = f;
            context.FoodLikes.AddObject(likefood);
            context.SaveChanges();
        }

        public void RemoveFoodLike(Model.FoodLike like, ref Model.NutriLeafDBEntities context)
        {
            if (context.FoodLikes.Where(p => p.FoodLikeID == like.FoodLikeID).Count() == 0)
            {
                context.FoodLikes.DeleteObject(context.FoodLikes.Where(p => p.FoodLikeID == like.FoodLikeID).First());
            }
        }

        public List<Model.Food> GetFoodLikedByUser(int userId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Food> food = new List<Model.Food>();
            List<Model.FoodLike> f = context.FoodLikes.Where(p => p.UserID == userId && p.Liked == true).ToList();
            foreach (Model.FoodLike li in f)
            {
                food.Add(li.Food);
            }
            return food;
        }
        // dont know




        public void UpdateFoodLike(Model.FoodLike foodLike, ref Model.NutriLeafDBEntities context)
        {
            //not impleemtnetd
        }
    }
}